<template>
  <div>
    <!-- 搜索 和 操作  begin -->
    <div style="margin-bottom: 10px">
      <el-row :gutter="4">
        <el-col :md="6">
          <el-input v-model="query.username" clearable size="small" placeholder="可用户名搜索" class="filter-item" @keyup.enter.native="toQuery" />
        </el-col>
        <el-col :md="6">
          <el-input v-model="query.mobile" clearable size="small" placeholder="可手机号搜索" class="filter-item" @keyup.enter.native="toQuery" />
        </el-col>
        <el-col :md="6">
          <el-input v-model="query.nickname" clearable size="small" placeholder="可姓名/昵称搜索" class="filter-item" @keyup.enter.native="toQuery" />
        </el-col>
        <el-col :md="3">
          <div style="margin-top: 2px">
            <el-button type="success" size="mini" @click="toQuery">搜索</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <!-- 搜索 和 操作  end -->
    <!-- 表格-->
    <el-table
      ref="table"
      v-loading="loading"
      :data="userData"
      border
      style="width: 100% "
      row-key="id"
      stripe
      tooltip-effect="dark"
      size="small"
      :highlight-current-row="true"
    >
      <!--<el-table-column type="selection" width="39" />-->
      <el-table-column prop="username" label="用户名" sortable="custom" />
      <el-table-column prop="mobile" label="手机号" sortable="custom" :show-overflow-tooltip="true" />
      <el-table-column prop="email" label="邮箱" sortable="custom" :show-overflow-tooltip="true" />
      <el-table-column prop="nickname" label="姓名" sortable="custom" />
      <el-table-column label="操作" width="120" header-align="center" align="center">
        <!--表格 内部操作-->
        <template slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="toAddMenuber(scope.row)"> 添加 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size.sync="page.size"
      :total="Number(page.total)"
      :current-page.sync="page.current"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChangeHandler($event)"
      @current-change="pageChangeHandler"
    />

  </div>
</template>

<script>
import { getNonMembers } from '@/api/sys/UserDep'
import CRUD, { crud } from '@crud/crud'
export default {
  name: 'UserList',
  mixins: [crud()],
  props: {
    depId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      visible: false,
      userData: [],
      permission: new Map(),
      page: {
        size: 10,
        total: 0,
        current: 1
      },
      query: {
        depId: '',
        username: null,
        mobile: null,
        nickname: null
      }
    }
  },
  watch: {
    depId(n, o) {
      if (n) {
        this.toQuery()
      }
    }
  },
  mounted() {
    this.toQuery()
  },
  methods: {
    // 查询
    toQuery() {
      this.getUsersData(this.depId)
    },
    // 当前页改变
    pageChangeHandler(e) {
      this.page.current = e
      this.toQuery()
    },
    // 每页条数改变
    sizeChangeHandler(e) {
      this.page.size = e
      this.page.current = 1
      this.toQuery()
    },
    // 获取用户数据
    getUsersData(depId) {
      this.selections = []
      this.loading = true
      this.query.depId = this.depId
      const param = {
        ...this.query,
        ...this.page
      }
      getNonMembers(param).then(res => {
        if (res.data && res.data.results) {
          this.userData = res.data.results
          this.page.total = res.data.total
        }
        setTimeout(() => {
          this.loading = false
        }, 200)
      }).catch(error => {
        console.log(error)
        this.loading = false
      })
    },
    toAddMenuber(row) {
      this.$emit('toAddMenuber', row.id, row.username, row.nickname)
    },
    [CRUD.HOOK.afterSubmit]() {
      this.toQuery()
    }
  }
}

</script>

<style scoped>

</style>
